package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.StoreBooksDto;
import com.dingreading.cloud.soms.entity.StoreBooks;
import com.dingreading.cloud.soms.entity.table.StoreBooksTableDef;
import com.dingreading.cloud.soms.mapper.StoreBooksMapper;
import com.dingreading.cloud.soms.service.StoreBooksService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StoreBooksServiceImpl extends BaseServiceImpl<StoreBooksMapper, StoreBooks, StoreBooksTableDef> implements StoreBooksService {

    @Override
    protected StoreBooksTableDef getTable() {
        return StoreBooksTableDef.storeBooks;
    }

    @Override
    public Page<StoreBooksDto> pageList(PageUtil pageUtil, Long storeId, String keyword, Integer status,
                                        Long journalId, Long bookLabelId, Integer isbnStatus) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (storeId != null) {
            condition.and(table.storeId.eq(storeId));
        }
        if (status != null) {
            condition.and(table.status.eq(status));
        }
        if (journalId != null) {
            condition.and(table.journalId.eq(journalId));
        }
        if (bookLabelId != null) {
            condition.and(table.bookLabelId.eq(bookLabelId));
        }
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.bookName.like(keyword)
                    .or(table.isbn.like(keyword))
                    .or(table.author.like(keyword))
                    .or(table.bookNum.like(keyword)));
        }

        QueryOrderBy[] queryOrders = null;
        if (isbnStatus != null && isbnStatus > 0) {
            if (isbnStatus.equals(1)) {
                condition.and(table.isbn.isNull().or(table.isbn.eq("")));
            } else if (isbnStatus.equals(2)) {
                QueryWrapper wrapper = QueryWrapper.create()
                        .select(table.isbn)
                        .from(table)
                        .where(table.storeId.eq(storeId).and(table.isbn.isNotNull().and(table.isbn.ne(""))))
                        .groupBy(table.isbn)
                        .having(QueryMethods.count(table.isbn).gt(1));
                condition.and(table.isbn.in(wrapper));

                queryOrders = new QueryOrderBy[]{table.isbn.desc(), table.editTime.desc()};
            }
        }

        if (queryOrders == null || queryOrders.length == 0)
            queryOrders = new QueryOrderBy[]{table.status.desc(), table.id.desc(), table.editTime.desc()};

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(queryOrders);

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreBooksDto.class);
    }


    @Override
    public List<StoreBooks> getByStoreIdAndBooksId(Long storeId, Long bookId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeId.eq(storeId))
                .and(table.booksId.eq(bookId));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UpdateChain.of(StoreBooks.class)
                .set(table.status, status)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<StoreBooks> getByStoreIdAndIsbnOrBookNum(Long storeId, String isbn) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeId.eq(storeId))
                .and(table.isbn.eq(isbn).or(table.bookNum.eq(isbn)));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public void updateStockNum(Long id, Integer num) {
        if (num == null) {
            num = 1;
        }
        UpdateChain.of(StoreBooks.class)
                .set(table.stockCount, table.stockCount.add(num))
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public StoreBooks getByStoreIdAndId(Long storeId, Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeId.eq(storeId).and(table.id.eq(id)));

        return getMapper().selectOneByCondition(condition);
    }


    @Override
    public int getStoreBookCnt(Long storeId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.sum(table.stockCount))
                .from(table)
                .where(table.storeId.eq(storeId));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }
}
